package thread.enumandlambda;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: wenjingyuan
 * @Date: 2022/10/29/20:40
 * @Description:
 */
public class LambdaTest {
    public static void main(String[] args) {
        //printTest();
        // interFaceTest();
       //  mapTest();
        //listSort();
       // distinctTest();
    }

    private static void distinctTest() {
        List<String> list=Arrays.asList("java","lambda","mysql","spring","lambda","springBoot");
        //jdk1.8之前
        for(String item:list){
            if(item.equals("lambda")){
                System.out.println("");
            }
        }
        //lambda之后
        List<String> finds=list.stream().filter(
                s->s.equals("lambda")).collect(Collectors.toList());
        System.out.println(finds);
    }

    private static void listSort() {
        //创建并初始化list
        List<Integer> list=Arrays.asList(5,3,6,8,2);
        System.out.println("排序前"+list);
//        list.sort(new Comparator<Integer>() {
//            @Override
//            public int compare(Integer o1, Integer o2) {
//                return o1-o2;
//            }
//        });
        list.sort((o1,o2)->{
            return o1-o2;
        });
        System.out.println("排序后"+list);
    }

    private static void printTest() {
        List<String> list= Arrays.asList("java","mysql","lambda","spring");
        //传统的打印方法
        for(String item:list){
            System.out.println(item);
        }
        System.out.println();
        //学完lambda之后
        list.forEach((item)->{
            System.out.println(item);
        });
    }

    private static void mapTest() {
        HashMap<String,String> map=new HashMap<String,String>(){{
            put("java","java value");
            put("mysql","mysql value");
        }};
        //jdk 1.8之前
        for(Map.Entry<String,String> item:map.entrySet()){
            System.out.println(item.getKey()+":"+item.getValue());
        }
        //lambda
        map.forEach((k,v)->{
            System.out.println(k+":"+v);
        });
    }

    /**
     * 函数式接口实例
     */
    private static void interFaceTest() {
        MyFunctionalInterface myFunctionalInterface=(o)->{
            for(Object item: o){
                System.out.println(item);
            }
        };
        myFunctionalInterface.myMethod("java","Mysql");
    }
}
